<template>
  <div>
    <el-dialog title="新增员工列表" :visible="addDialogVisible" @close="close">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="name" label="员工名字:">
          <el-input v-model="formData.name" placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="age" label="员工年龄:">
          <el-input v-model.number="formData.age" placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="phone" label="员工手机号:">
          <el-input v-model="formData.phone" placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="idNumber" label="员工身份证">
          <el-input v-model="formData.idNumber" placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="员工性别:">
          <el-input v-model="formData.sex" placeholder=""></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="btnOk"
                >确认</el-button
              >
              <el-button size="mini" type="primary" @click="close"
                >取消</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addemployeeApi } from "@/api/employee";
export default {
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formData: {
        name: "",
        age: "",
        phone: "",
        idNumber: "",
        sex: "",
      },
      rules: {
        name: [
          { required: true, message: "员工名字不能为空", trigger: "blur" },
          // 假设我们允许名字长度在2到20个字符之间
          {
            min: 2,
            max: 20,
            message: "名字长度必须在2到20个字符之间",
            trigger: "blur",
          },
          // 如果只允许字母输入
          {
            pattern: /^[a-zA-Z\u4e00-\u9fa5]+$/,
            message: "名字只能包含字母和中文字符",
            trigger: "blur",
          },
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" },
          {
            type: "number",
            min: 18,
            max: 100,
            message: "年龄必须在 18 到 100 岁之间",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请填写正确的手机号",
            trigger: "blur",
          },
        ],
        idNumber: [
          { required: true, message: "员工身份证不能为空", trigger: "blur" },
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "请填写正确的身份证号",
            trigger: "blur",
          },
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" },
          {
            pattern: /^(男|女)$/,
            message: "请输入有效的性别",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 关闭弹层
    close() {
      // 重置表单数据
      this.$refs.formRef.resetFields();
      // 清空表单数据
      this.formData = {};
      // 通知父组件关闭弹层
      this.$emit("update:addDialogVisible", false);
    },
    // 点击确认
    async btnOk() {
      // 表单校验
      console.log(this.formData);
      await this.$refs.formRef.validate();
      const res = await addemployeeApi(this.formData);
      // console.log(res);
      this.close();
      if (res.code === 1) {
        this.$message.success("添加成功");
      }
      // 通知父组件更新数据
      this.$emit("employeeList");
    },
  },
};
</script>

<style scoped></style>
